//
//  Permutator.m
//  FilePermutator
//
//  Created by Christopher Kemsley on 4/7/12.
//  Copyright (c) 2012 iAppolopolis. All rights reserved.
//

#import "Permutator.h"




@implementation PermutatorFile {
@public
	NSString*	name ;
	NSString*	path ;
	__unsafe_unretained PermutatorEntry*	parent ;
}
@synthesize name , path , parent ;

- (id)	initWithName:(NSString*)inName path:(NSString*)inPath {
	if ( (self = [super init]) )
		{
		name = inName ;
		path = inPath ;
		}
	return self ;
}

- (NSString*)	description {
	return [ [ [super description] stringByAppendingFormat:@"{ name=\"%@\" path=\"%@\" }",name,path ]  stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n" ];
}


@end




@implementation PermutatorEntry {
	NSString*	name ;
	NSArray*	files ;
}
@synthesize name , files ;

- (id)	initWithName:(NSString*)inName files:(NSArray*)inEntries {
	if ( (self = [super init]) )
		{
		name	=	inName ;
		files	=	inEntries ;
		
		for ( PermutatorFile * file in inEntries )
			{
			if ( file -> parent != 0 )
				[NSException raise:NSInvalidArgumentException format:@"Added file %@ to entry, but already has parent entry %@",file,file->parent] ;
			file->parent = self ;
			}
		}
	return self ;
}
- (void)	dealloc {
	for ( PermutatorFile * file in files )
		{
		if ( file -> parent != self )
			[NSException raise:NSInvalidArgumentException format:@"File %@ in entry %@ doesn't have us as a parent; instead, has %@",file,self,file.parent] ;
		file->parent = 0 ;
		}
}

- (NSString*)	description {
	return [ [ [super description] stringByAppendingFormat:@"{ name=\"%@\" files=%@ }",name,files ] stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n" ];
}

@end




@implementation Permutator {
	NSArray*	entries ;
}
@synthesize entries ;

- (id)	initWithEntries:(NSArray*)inEntries {
	if ( (self = [super init]) )
		{
		entries = [inEntries mutableCopy] ;
		}
	return self ;
}

- (NSString*)	description {
	return [ [ [super description] stringByAppendingFormat:@"{ entries=%@ }",entries ] stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n" ] ;
}

@end


